import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hi_common/hi_common.dart';

class CenterTitleAppBar extends StatefulWidget implements PreferredSizeWidget {
  final dynamic title;
  final List<Widget> actions;
  final bool isShowDivider;
  final VoidCallback onBack;
  final Brightness brightness;
  final String backConfirmationText;

  const CenterTitleAppBar({
    Key key,
    this.title,
    this.actions,
    this.isShowDivider = true,
    this.onBack,
    this.brightness = Brightness.light,
    this.backConfirmationText,
  }) : super(key: key);

  @override
  _CenterTitleAppBarState createState() => _CenterTitleAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(88.w);
}

class _CenterTitleAppBarState extends State<CenterTitleAppBar> {
  @override
  Widget build(BuildContext context) {
    final Brightness brightness = widget.brightness;

    final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle,
      child: Container(
        height: 88.w + MediaQuery.of(context).padding.top,
        padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top, left: 20.w, right: 20.w),
        decoration: BoxDecoration(
          border: widget.isShowDivider ? Border(bottom: BorderSide(color: ColorConstant.e6e6, width: 1.px)) : null,
        ),
        alignment: Alignment.center,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Expanded(child: _buildBackIcon()),
            _buildTitle(),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: widget.actions ?? [],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBackIcon() {
    final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
    final bool canPop = parentRoute?.canPop ?? false;

    if (canPop) {
      return GestureDetector(
        onTap: () {
          if (widget.onBack != null) {
            widget.onBack();
          } else {
            if (widget.backConfirmationText != null) {
              showAlert(
                context,
                content: widget.backConfirmationText,
                onConfirm: (ctx) {
                  Navigator.of(ctx).pop();
                  Navigator.of(context).pop();
                },
                onCancel: (context) {
                  Navigator.of(context).pop();
                },
              );
            } else {
              Navigator.pop(context);
            }
          }
        },
        behavior: HitTestBehavior.translucent,
        child: Container(
          padding: EdgeInsets.all(10.w),
          alignment: Alignment.centerLeft,
          child: BaseImage(
            'assets/image/arrow_left_icon.png',
            width: 16.w,
            height: 34.w,
          ),
        ),
      );
    }

    return SizedBox();
  }

  Widget _buildTitle() {
    if (widget.title is String) {
      return Text(
        widget.title,
        style: TextStyleConstant().appBarTitle,
      );
    }

    if (widget.title is Widget) {
      return widget.title;
    }

    return SizedBox();
  }
}
